from typing import Optional

from pydantic import BaseModel
from datetime import datetime


class BlogQuery(BaseModel):
    # 类别
    category: Optional[str] = None
    # 标题
    title: Optional[str] = None
    # 年份
    year: Optional[int] = None


class BlogResponse(BaseModel):
    id: Optional[int] = None
    title: Optional[str] = None
    topValue: Optional[int] = None
    isGreat: Optional[bool] = None
    category: Optional[str] = None
    summary: Optional[str] = None
    content: Optional[str] = None
    wordCount: Optional[int] = None
    readDuration: Optional[float] = None
    visitCount: Optional[int] = None
    createTime: Optional[datetime] = None
    updateTime: Optional[datetime] = None

    class Config:
        from_attributes = True
        json_encoders = {
            # 自定义 datetime 类型的序列化格式
            datetime: lambda dt: dt.strftime('%Y-%m-%d %H:%M:%S')
        }


class BlogCategoryResponse(BaseModel):
    name: str
    count: int

    class Config:
        from_attributes = True


class BlogStatsResponse(BaseModel):
    blogCount: int
    categoryCount: int
    wordCount: int


class BlogLatestResponse(BaseModel):
    id: int
    title: str

    class Config:
        from_attributes = True


class BlogCommentCreate(BaseModel):
    blogId: int
    parentId: int
    name: str
    website: Optional[str] = None
    content: str


class BlogCommentResponse(BlogCommentCreate):
    id: int
    ipAddress: str
    userAgent: str
    createTime: datetime

    class Config:
        from_attributes = True
        json_encoders = {
            # 自定义 datetime 类型的序列化格式
            datetime: lambda dt: dt.strftime('%Y-%m-%d %H:%M:%S')
        }
